﻿<META http-equiv="refresh" content="<?php echo rand(5,10); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

date_default_timezone_set("Asia/Shanghai");
@ $matchId = $_GET["matchId"];
@ $type = $_GET["type"];
@ $date = $_GET["date"];

if ($matchId == "") {
	return;
}
if ($date == "") {
	$date = date("ym");
}

//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

if ($type == "NBA") {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "http://sports.sinajs.cn/" . $date . "/content" . $matchId . ".js?" . rand());
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchDetail = curl_exec($ch);
	curl_close($ch);
	$matchDetail = iconv("GBK", "UTF-8", $matchDetail);
	$matchDetail = str_replace("b_live_draw_table(", "", $matchDetail);
	$matchDetail = str_replace("}])", "}]", $matchDetail);
	$matchDetail = str_replace("更多标签,", "\"}},", $matchDetail);
	$matchDetail = json_decode($matchDetail);
} else {
	$matchDetail = file_get_contents("http://data.sports.sina.com.cn/livecast/log/log_" . $matchId . ".js?" . rand());
	$matchDetail = iconv("GBK", "UTF-8", $matchDetail);
	$matchDetail = str_replace("show_log(", "", $matchDetail);
	$matchDetail = str_replace("}])", "}]", $matchDetail);
	
	$matchDetail = json_decode($matchDetail);
}

//先清空当前比赛场次的数据
$sql = "delete from live_score where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_score (scoreId, matchId, homeTeamScore, visitTeamScore, scoreTime, scoreAction) VALUES";

$i = 0;
$id = 0;
$match_st = "第一节";
$match_m = "未开赛";
$match_s1 = 0;
$match_s2 = 0;
foreach ($matchDetail as $match) {
	if (isset ($match->q)) {
		
		//不插入不包含文字内容的
		if (!isset ($match->m)) {
			continue;
		}
		//不插入第一位是...的内容
		if (!isset ($match->c) || $match->c == "0") {
			continue;
		}
		//不插入第一位是...的内容
		if (strpos($match->m, "...") === 0) {
			$match->m = str_replace("...", "", $match->m);
		}
		//不插入:的内容
		if (strpos($match->m, ":")) {
			continue;
		}
		//不插入第一位是.的内容
		if (strpos($match->m, ".") === 0) {
			continue;
		}
		//不插入包含图片的内容
		if (strpos($match->m, "img") > 0) {
			continue;
		}
		//不插入0:0的内容
		if (($match->s->s1 == "0" && $match->s->s2 == "0") && $match->q != "0") {
			continue;
		}
		
		$match->m = str_replace("\\","",$match->m);
		if($match->q == "0"){
			$match_st = "未赛";
			$match_m = $match->m;
		}else{
			if($match->q == "9"){
				$match_st = "完赛";
			}else{
				$match_st = "第" . $match->q . "节";
				if ($type == "NBA") {
					if (intval($match->q) > 4) {
						$match_st = "加时" . (intval($match->q) - 4);
					}
				} else {
					if (intval($match->q) > 10) {
						$match_st = "加时" . (intval($match->q) - 10);
					}
				}
			}
			
			if ($match_s1 < $match->s->s1 || $match_s2 < $match->s->s2 || strpos($match->m, "结束") || strpos($match->m, "开始")) {
				$match_s1 = $match->s->s1;
				$match_s2 = $match->s->s2;
				$match_m = $match->m;
			} else {
				continue;
			}
		}
		$id = $i++;
		$sql .= "('" . $i . "', '" . $matchId . "', '" . $match_s1 . "','" . $match_s2 . "', '" . $match_st . "', '" . $match_m . "'),";

	}
}
echo "<table width='100%'>";
echo "<tr>";
echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_st . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_s1 . " : " . $match_s2 . "</td>";
echo "</tr><tr>";
echo "<td>" . $match_m . "</td>";
echo "</tr>";
echo "</table>";

$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

if($id > 0){
	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}
?>